#include <Def.h>
#include <QtConcurrent/qtconcurrentrun.h>
#include <osg/renderInfo>
#include <osgDB/writeFile>
#include "SnapImage.h"

void SnapImage::operator()(osg::RenderInfo& renderInfo)const
{
	if(!snapImage || file.empty()) 
		return;

	osg::Camera* camera = renderInfo.getCurrentCamera();
	osg::Viewport* viewport = camera ? camera->getViewport() : 0;

	if(viewport && image.valid())
	{
		image->readPixels(int(viewport->x()),int(viewport->y()),int(viewport->width()),int(viewport->height()),
			GL_RGBA,
			GL_UNSIGNED_BYTE);
		
		auto function = [&]()
		{
			osgDB::writeImageFile(*image,file);
		};

		QtConcurrent::run(function);
	}

	snapImage = false;
}